package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Goods;
import cn.wolfcode.car.business.domain.StockBillItem;
import cn.wolfcode.car.business.domain.Store;
import cn.wolfcode.car.business.query.GoodsQuery;
import cn.wolfcode.car.business.service.IGoodsService;
import cn.wolfcode.car.business.service.IStoreService;
import cn.wolfcode.car.business.service.impl.StoreServiceImpl;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.util.uuid.UUID;
import cn.wolfcode.car.common.web.AjaxResult;
import com.github.pagehelper.util.StringUtil;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("business/goods")
public class GoodsController {
    //模板前缀
    private static final String prefix = "business/goods/";

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IStoreService storeService;

    //页面------------------------------------------------------------
    //列表
    @RequestMapping("/listPage")
    public String listPage(Model model,Integer type,Long storeId){
        model.addAttribute("stores", storeService.list());
        if (storeId !=null) {
        model.addAttribute("storeId", Integer.valueOf(storeId.toString()));
        }

        return prefix + "list";
    }

    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("goods", goodsService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Goods> query(GoodsQuery qo){
        return goodsService.query(qo);
    }

    //新增
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Goods goods){
        goodsService.save(goods);
        return AjaxResult.success();
    }

    //编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Goods goods){
        goodsService.update(goods);
        return AjaxResult.success();
    }

    //删除
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        goodsService.deleteBatch(ids);
        return AjaxResult.success();
    }

    // 上传图片 uploadFile
    @PostMapping("/uploadFile")
    @ResponseBody
    public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
        String url = goodsService.uploadFileMinio(file);
        return AjaxResult.success("操作成功",url);
    }

    // 物品新增到明细列表
    @RequestMapping("/goodsToBill")
    @ResponseBody
    public AjaxResult goodsToBill(String ids){
       goodsService.goodsSave(ids);
        return AjaxResult.success();
    }
}
